package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjOrganizeEmp;
import com.zb.mapper.ZjOrganizeEmpMapper;
import com.zb.service.ZjOrganizeEmpService;
import com.zb.view.OrganizeEmpView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商家组织人员表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-22
 */
@Service
public class ZjOrganizeEmpServiceImpl extends ServiceImpl<ZjOrganizeEmpMapper, ZjOrganizeEmp> implements ZjOrganizeEmpService {
    @Resource
    private ZjOrganizeEmpMapper organizeEmpMapper;

    // 保存或更改组织管理员
    @Override
    public void saveOrganizeManagerEmp(Long custAccId, Long organizeId, Long manegerEmpId) {
        ZjOrganizeEmp organizeEmp = organizeEmpMapper.getOrganizeManagerEmpByOrganizeId(organizeId);
        if (null != organizeEmp) {
            organizeEmp.setEmpId(manegerEmpId);
            this.updateById(organizeEmp);
            return;
        }
        organizeEmp = new ZjOrganizeEmp();
        organizeEmp.setId(IdWorker.getId());
        organizeEmp.setCustAccId(custAccId);
        organizeEmp.setOrganizeId(organizeId);
        organizeEmp.setEmpId(manegerEmpId);
        organizeEmp.setManager(true);
        this.save(organizeEmp);
    }

    // 保存组织人员
    @Override
    public void saveOrganizeEmp(Long custAccId, Long organizeId, Long empId) {
        ZjOrganizeEmp organizeEmp = organizeEmpMapper.getOrganizeEmpByEmpId(empId);
        if (null != organizeEmp) {
            organizeEmp.setOrganizeId(organizeId);
            this.updateById(organizeEmp);
            return;
        }
        organizeEmp = new ZjOrganizeEmp();
        organizeEmp.setId(IdWorker.getId());
        organizeEmp.setCustAccId(custAccId);
        organizeEmp.setOrganizeId(organizeId);
        organizeEmp.setEmpId(empId);
        organizeEmp.setManager(false);
        this.save(organizeEmp);
    }

    // 根据[组织ID]&[人员ID集合]查询是否存在关联
    @Override
    public Integer existByEmpIdList(Long organizeId, List<Long> empIdList) {
        return organizeEmpMapper.existByEmpIdList(organizeId, empIdList);
    }

    // 根据[组织人员ID]集合删除
    @Override
    public void removeByOrganizeIdAndEmpId(Long organizeId, List<Long> empIdList) {
        organizeEmpMapper.removeByOrganizeIdAndEmpId(organizeId, empIdList);
    }

    // 根据职员ID获取组织职员信息
    @Override
    public Long getOrganizeIdByEmpId(Long empId) {
        Long organizeId = organizeEmpMapper.getOrganizeIdByEmpId(empId);
        return organizeId;
    }

    // 根据人员ID获取职员组织关系
    @Override
    public ZjOrganizeEmp getByEmpId(Long empId) {
        QueryWrapper<ZjOrganizeEmp> wrapper = new QueryWrapper<>();
        wrapper.select("id", "organize_id", "emp_id", "is_manager AS manager")
                .eq("emp_id", empId);
        return this.getOne(wrapper);
    }

    // 根据部门组织ID获取部门组织主管ID
    @Override
    public Long getOrganizeManagerEmpIdByOrganizeId(Long organizeId) {
        QueryWrapper<ZjOrganizeEmp> wrapper = new QueryWrapper<>();
        wrapper.select( "emp_id")
                .eq("organize_id", organizeId)
                .eq("is_manager", true);
        ZjOrganizeEmp emp = this.getOne(wrapper);
        return null != emp? emp.getEmpId() : 0L;
    }
}
